#readin ucsc refflat file and asigning the gene cordinates to each gene
open(BUFF,"ucsc_genes") or die "no file ucsc_genes found\n";
while(<BUFF>)
{
        chomp($_);
        @array = split("\t",$_);
        #$probe_gene{$array[0]} = $array[1];
	$gene{$array[0]} ="$array[2]\t$array[4]\t$array[5]";
}
#emptying the array
@array =();

#readin the gene info file from ncbi to map gene synonyms 
open(BUFF,"Homo_sapiens.gene_info") or die "no file found Homo_sapiens.gene_info\n";
while(<BUFF>)
{
        chomp($_);
        @array = split("\t",$_);
	if($array[4] ne '-')
	{
		@barray = split(/\|/,$array[4]);
		unshift(@barray,$array[2]);
		#print "@barray\n";	
		$a = "";
		for($i=0;$i<@barray;$i++)
		{
			if(exists($gene{$barray[$i]}))
			{
				$a = $gene{$barray[$i]};
			}
		}
		if($a ne "")
		{
			for($i=0;$i<@barray;$i++)
                	{
				$gene2{$barray[$i]} = $a;
                	}
		}
	}	
}
@array =();
#Reading the gene Probe map file illumina nnotation
open(BUFF,"feature.info.eqtl.txt") or die "no file illumina_genes found\n";
$line = <BUFF>;
while(<BUFF>)
{
        chomp($_);
	$_ =~ s/"//g;
        @array = split("\t",$_);
	$probe_gene{$array[1]} = $array[2];
}

@array =();


#Reading the micro rna file provided by kristen to asign sopme genes
open(BUFF,"genelist_missing_mir.txt") or die "no file genelist_missing_mir.txt found\n";
#$line = <BUFF>;
while(<BUFF>)
{
        chomp($_);
        $_ =~ s/"//g;
        @array = split("\t",$_);
        $probe_gene{$array[1]} = $array[0];
	$gene3{$array[0]} = "$array[2]\t$array[3]\t$array[4]";
}

@array =();

#readin the Expr file and filtering and mapping of the gene coordinates is done
open(BUFF,"GE_pre.txt") or die "no file found\n";
open(WRBUFF,">GE.txt") or die "not able to write GE.txt\n";
open(GLOC,">geneloc.txt") or die "not able to write geneloc.txt\n";
$line = <BUFF>;
print WRBUFF $line;
print GLOC "geneid\tchrm_probe\ts1\ts2\n";
#$n = 0;
while(<BUFF>)
{
	chomp($_);
	@array = split("\t",$_);
	
	if(exists($probe_gene{$array[0]}))
	{
		$ind = 0;
		$gene = "";
		#print "$probe_gene{$array[0]}\n";
		if(exists($gene{$probe_gene{$array[0]}}))
		{
			#print "$array[0]\t$gene{$probe_gene{$array[0]}}\n";
			#$n++;
			$ind =1;
			$gene = "$array[0]\t$gene{$probe_gene{$array[0]}}";
		}
		elsif(exists($gene2{$probe_gene{$array[0]}}))
                {
                        #print "$array[0]\t$gene2{$probe_gene{$array[0]}}\n";
                        #$n++;
			$ind = 1;
			$gene = "$array[0]\t$gene2{$probe_gene{$array[0]}}";
                }
		elsif(exists($gene3{$probe_gene{$array[0]}}))
                {
                        #print "$array[0]\t$gene2{$probe_gene{$array[0]}}\n";
                        #$n++;
			$ind =1;
			$gene = "$array[0]\t$gene3{$probe_gene{$array[0]}}";
                }

		else
		{
			print "no included $probe_gene{$array[0]}\t$array[0]\n";
		}
		if($ind == 1 && $gene ne "")
		{
			@array_g =split("\t",$gene);
			$array_g[1] =~ s/chr//g;
			$array_g[1] =~ s/X/23/g;
			$array_g[1] =~ s/Y/24/g;
			$array_g[1] =~ s/6_cox_hap1/27/g;
                        $array_g[1] =~ s/6_qbl_hap2/28/g;
			$array_g[1] =~ s/5_h2_hap1/29/g;
			if($array_g[1] =~ m/_random/)
			{
				$array_g[1] =~ s/_random//g;
				$array_g[1] =$array_g[1]+30;
			}	
			$gene = join("\t",@array_g);
			print WRBUFF $_."\n";
			print GLOC "$gene\n";
		}
		
	}
	else
	{
		die "no probe-gene mapping $array[0]\n";
	}
}
#print "numberof probes $n $. don't have coordinates\n";
